/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import model.dominio.Professor;
import model.dominio.Turma;
/**
 *
 * @author ronan
 */
public class DaoProfessor extends Dao {
    
    public Professor buscarLogin (String cpf,String senha){
        EntityManager em = super.getEntityManager();
        try {
            String strSQL = "SELECT a FROM Professor a ";
            if (cpf!= null && senha!= null) {
                strSQL += "WHERE a.cpf='" + cpf + "' AND a.senha='" + senha + "' ";
            }else{
                return null;
            }
            strSQL += "ORDER BY NOME ASC";
            em.getTransaction().begin();

            Query query = em.createQuery(strSQL);

            return (Professor) query.getSingleResult();
        } catch (PersistenceException e) {
            return null;
        } finally {
            em.close();
        } 
    }
    
    public List<Professor> buscarProfessor(Professor professor) {
        EntityManager em = super.getEntityManager();
        try {
            String strSQL = "SELECT a FROM Professor a ";
            if (professor.getCpf() != null) {
                strSQL += "WHERE a.cpf='" + professor.getCpf() + "' ";
            } else if (professor.getNome() != null) {
                strSQL += "WHERE a.nome LIKE '" + professor.getNome() + "%' ";
            }
            strSQL += "ORDER BY NOME ASC";
            em.getTransaction().begin();

            Query query = em.createQuery(strSQL);

            return (List<Professor>) query.getResultList();
        } catch (PersistenceException e) {
            return null;
        } finally {
            em.close();
        }
    }
    
    public List<Turma> buscarTurmas (Professor professor) {
        EntityManager em = super.getEntityManager();
        try {
            String strSQL = "SELECT a FROM Turma a ";
            List<Turma> turmas = new ArrayList<>() ;
            for (Turma t : turmas) {
                if (t.getProfessor() == professor) {
                    strSQL += "WHERE a.professor='" + t.getProfessor() + "' ";
                }
            }
            strSQL += "ORDER BY NOME ASC";
            em.getTransaction().begin();

            Query query = em.createQuery(strSQL);

            return (List<Turma>) query.getResultList();
        } catch (PersistenceException e) {
            return null;
        } finally {
            em.close();
        }
    }
    
}
